﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ProjectVLIB_LAB04.KetNoiDB;
using System.Data.SqlClient;
using ProjectVLIB_LAB04.VOL;

namespace ProjectVLIB_LAB04.BLL
{
    class LoaiHangBL
    {
        private DataAccess dataAccess;

        public LoaiHangBL()
        {
            dataAccess = new DataAccess();
        }

        /// <summary>
        /// 1. Hiển thị danh sách loai hang
        /// </summary>
        /// <returns></returns>
        public DataTable GetData()
        {
            // cau query select
            string sql = "SELECT MaLoaiHang, TenLoaiHang FROM tbl_LoaiHang ORDER BY MaLoaiHang DESC ";

            DataTable dt = new DataTable();
            // do toan bo du lieu vao datatable
            dt = dataAccess.GetData(sql);
            return dt;
        }

        /// <summary>
        /// 2. Thêm loai hang
        /// </summary>
        /// <param name="obj">LoaiHang</param>
        /// <returns>true</returns>
        public bool InsertData(LoaiHang obj)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@TenLoaiHang", SqlDbType.NVarChar) { Value = obj.TenLoaiHang };
           
            // cau query select
            string sql = "INSERT INTO tbl_LoaiHang  (TenLoaiHang) VALUES (@TenLoaiHang)";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 3. Xóa Nd
        /// </summary>
        /// <param name="MaNguoiDung">MaLoaiHang INT</param>
        /// <returns>true</returns>
        public bool DeleteData(int MaLoaiHang)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaLoaiHang", SqlDbType.Int) { Value = MaLoaiHang };
            // cau query select
            string sql = "DELETE FROM tbl_LoaiHang WHERE MaLoaiHang = @MaLoaiHang";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 4. sửa loai hang
        /// </summary>
        /// <param name="obj">Loai Hang</param>
        /// <returns>true</returns>
        public bool UpdateData(LoaiHang obj)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@TenLoaiHang", SqlDbType.NVarChar) { Value = obj.TenLoaiHang };
            param[1] = new SqlParameter("@MaLoaiHang", SqlDbType.Int) { Value = obj.MaLoaiHang };

            // cau query select
            string sql = "UPDATE tbl_LoaiHang SET TenLoaiHang = @TenLoaiHang WHERE MaLoaiHang = @MaLoaiHang";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 5. Tìm theo ma loai hang
        /// </summary>
        /// <param name="MaLoaiHang"></param>
        /// <returns></returns>
        public DataTable GetDataById(int MaLoaiHang)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaLoaiHang", SqlDbType.Int) { Value = MaLoaiHang };
            // cau query select
            string sql = "SELECT * FROM tbl_LoaiHang WHERE MaLoaiHang = @MaLoaiHang";
            DataTable dt = dataAccess.GetData(sql, param);

            return dt;
        }

        // 6. Login
        // 7. Logout
    }
}
